<template>
  <el-dialog
    top="5vh"
    width="1000px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />
  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'course',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        name: '',
        description: '',
        logo: '',
        location: '',
        schedule: '',
        grade: [
            '1', '2', '3', '4', '5', '6'
        ],
        teacher_name: '',
        teacher_info: '',
        total_count: 0,
        status: 1,
        id: undefined,
      },
      rules: {
        name: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
        grade: [{ required: true, message: '请输入上课年级', trigger: 'blur' }],
        teacher_name: [{ required: true, message: '请输入教师姓名', trigger: 'blur' }],
        total_count: [{ required: true, message: '请输入人数', trigger: 'blur' }],
        teacher_info: [{ required: true, message: '请输入教师简介', trigger: 'blur' }],
        description: [{ required: true, message: '请输入课程简介', trigger: 'blur' }],
      },
      formList: [
        {
          label: '课程名称',
          prop: 'name',
          name: 'input',
          clearable: true,
          colSpan: 8
        },
        {
          label: '上课地点',
          prop: 'location',
          name: 'input',
          clearable: true,
          colSpan: 8
        },
        {
          label: '人数',
          prop: 'total_count',
          name: 'input',
          type: 'number',
          clearable: true,
          colSpan: 8
        },
        {
          label: '上课时间',
          prop: 'schedule',
          name: 'input',
          clearable: true,
          colSpan: 8,
        },
        {
          label: '上课年级',
          prop: 'grade',
          name: 'checkbox',
          clearable: true,
          colSpan: 16,
          options: [
            { label: '一年级', value: '1' },
            { label: '二年级', value: '2' },
            { label: '三年级', value: '3' },
            { label: '四年级', value: '4' },
            { label: '五年级', value: '5' },
            { label: '六年级', value: '6' }
          ]
        },
        {
          label: '课程简介',
          prop: 'description',
          name: 'input',
          clearable: true,
          type: 'textarea',
          colSpan: 24,
        },
        {
          label: '封面',
          prop: 'logo',
          name: 'file',
          clearable: true,
          colSpan: 8,
        },
        {
          label: '图片',
          prop: 'image',
          name: 'file',
          multiple: true,
          clearable: true,
          colSpan: 16,
          limit: 1
        },
        {
          label: '教师姓名',
          prop: 'teacher_name',
          name: 'input',
          clearable: true,
          colSpan: 8
        },
        {
          label: '教师简介',
          prop: 'teacher_info',
          name: 'rich',
          type: 'textarea',
          clearable: true,
          colSpan: 24,
          rows: 4
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          colSpan: 12,
          options: [
            { label: '正常', value: 1 },
            { label: '禁用', value: 0 }
          ]
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {
      this.ruleForm[key] = val
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
            grade: res.data.grade.split(','),
            schedule: res.data.schedule.split(',')
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, {
        ...this.ruleForm,
        grade: this.ruleForm.grade.join(','),
      }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
